﻿using DH.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DH.Data.ORM.EF.Extensions
{
    
   public static  class EFQuery_Extension
    {
        /// <summary>
        /// 分页扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="q"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static PagedData<T> TakePageData<T>(this IQueryable<T> q, Pagination page)
        {
            PagedData<T> pageData = page.ToPagedData<T>();

            page.Page = page.Page != 0 ? page.Page - 1 : 0;
            if (page.PageSize < 0)
            {
                pageData.DataList = q.ToList();
            }
            else {
                pageData.DataList = q.Skip(page.Page * page.PageSize).Take(page.PageSize).ToList();
            }
            
            pageData.TotalCount = q.Count();

            return pageData;
        }

        
    }
}
